
from multiprocessing import shared_memory


class CommonMemory:

    def __init__(self, name:str="CommonMemory", length:int=8192, STR_LENG:int=128):
        '''
        创建或者获取CommonMemory的共享内存列表
        :param name: 共享内存名称
        :param length: 共享内存列表长度
        :return: 共享内存对象
        '''

        self.STR_LENG = STR_LENG

        try:
            self._mem = shared_memory.ShareableList([
                # (("\0" * self.STR_LENG)                                 # 将字符串位固定为STR_LENG长度
                ((b'\0' * 201314 if (i - 4) % 8 == 1 else "\0" * self.STR_LENG)  # 将字符串位固定为STR_LENG长度
                 if (i == 1 or i == 4 or (i - 4) % 8 == 1 or (i - 4) % 8 == 2 or (i - 4) % 8 == 4)
                 else None)
                for i in range(length)], name=name)

        except FileExistsError:
            self._mem = shared_memory.ShareableList(name=name)


        # _mem[0]: 状态码         int
        # _mem[1]: 摄像头编号      int
        # _mem[2]: 方向编号       int
        # _mem[3]: 有几辆车       int
        # _mem[4]: 当前帧时间      string
        # _mem[5~12]: 第一辆车的信息
        #   该车截图保存位置        string
        #   车牌                  string
        #   车类型                 int
        #   车颜色                 string
        #   车道                   int
        #   距入口距离              int
        #   经纬度x                double
        #   经纬度y                double
        # _mem[13~20]: 第二辆车的信息
        #   该车截图保存位置        string
        #   车牌                  string
        #   车类型                 int
        #   车颜色                 int
        #   车道                   int
        #   距入口距离              int
        #   经纬度x                double
        #   经纬度y                double
        # ...
        # 直到_mem[3]辆车信息全写完结束
        # ...


    '''
    设置 _mem[0] 状态码
    '''
    def setWaitingFlag(self):
        self._mem[0] = 101

    def setReadingFlag(self):
        self._mem[0] = 102

    def setSendingOverFlag(self):
        self._mem[0] = 201

    def setSendingFlag(self):
        self._mem[0] = 202

    '''
    判断状态码，查看是否能写入，或者读取
    '''
    def canRead(self):
        res = None
        while res is None:
            try:
                res = self._mem[0] == 201
            except Exception as e:
                continue

        return res


    def canWrite(self):
        res = None
        while res is None:
            try:
                res = self._mem[0] == 101
            except Exception as e:
                continue

        return res

    '''
    读写数据
    '''
    def setData(self, index:int, data):
        # 将字符串补长度至STR_LENG
        # if isinstance(data, str):
        #
        #     data = data + (chr(0) * (self.STR_LENG - len(data)))
        #     print(data)

        self._mem[index] = data
        # print(self._mem[index])

        # 写入成功返回true,目前假定总是会写入成功
        return True


    def getData(self, index:int):

        data = self._mem[index]
        # 去除字符串中补的chr(0)
        # if isinstance(data, str):
        #     data = data[0:data.find(chr(0))]

        return data


    '''
    判断和读取是否开始重整双方程序
    '''
    def setReform(self):
        self._mem[0] = 999


    def isReform(self):
        res = None
        while res is None:
            try:
                res = self._mem[0] == 999
            except Exception as e:
                continue

        return res